﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MySql.Data.MySqlClient;
using MySql.Data.Types;

namespace Agendapunten
{
    public partial class AddAgendapunten : Form
    {
        private MySqlDataReader reader2;
    
        public AddAgendapunten()
        {
            InitializeComponent();
            getAgenda();
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e) {}

        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            String currentDate = (String) listBox1.SelectedItem;
            getNote(currentDate);     
        }

        public void getAgenda()
        {
            try
            {
                int projectID = 1;
                MySqlConnection grabConnection = new MySqlConnection("Server=instance8003.db.xeround.com;Port=6006;Database=GrabCollab;Uid=cookicom0;Pwd=pizzahut91;");
                MySqlCommand agendaGet = new MySqlCommand("SELECT agendaID, date FROM agenda WHERE projectID=@projectID", grabConnection);
                agendaGet.Parameters.Add(new MySqlParameter("projectID", projectID));
                grabConnection.Open();
                MySqlDataReader reader = agendaGet.ExecuteReader();

                if (reader.Read() != false)
                {
                    if (reader.IsDBNull(0) == true)
                    {
                        grabConnection.Close();
                        reader.Dispose();
                        agendaGet.Dispose();
                    }
                    else
                    {
                        while (reader.Read())
                        {
                            int nummer = reader.GetInt32(0);
                            String date = reader.GetString(1);
                            listBox1.Items.Add(date);
                        }
                    }

                grabConnection.Close();
                reader.Dispose();
                agendaGet.Dispose();   
                }
            }       
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
                MessageBox.Show("Sql error detected!");
            }  
        }

        public void getNote(String currentDate)
        {
            try
            {
                int projectID = 1;
                MySqlConnection grabConnection = new MySqlConnection("Server=instance8003.db.xeround.com;Port=6006;Database=GrabCollab;Uid=cookicom0;Pwd=pizzahut91;");
                MySqlCommand noteGet = new MySqlCommand("SELECT note FROM agenda WHERE projectID=@projectID AND date=@currentDate", grabConnection);
                noteGet.Parameters.Add(new MySqlParameter("projectID", projectID));
                noteGet.Parameters.Add(new MySqlParameter("currentDate", currentDate));
                grabConnection.Open();
                reader2 = noteGet.ExecuteReader();

                if (reader2.Read() != false)
                {
                    if (reader2.IsDBNull(0) == true)
                    {
                        grabConnection.Close(); 
                        reader2.Dispose();
                        noteGet.Dispose();
                    }
                    else
                    {                           
                        string note = reader2.GetString(0);
                        richTextBox1.Text = note;

                        grabConnection.Close();
                        reader2.Dispose();
                        noteGet.Dispose();
                    }
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
                MessageBox.Show("Sql error detected!");
            }
        }

        private void richTextBox1_TextChanged(object sender, EventArgs e)
        {}

        private void label3_Click(object sender, EventArgs e) {}

        private void button2_Click(object sender, EventArgs e)
        {
            Agenda.GrabCollabAgenda nieuwePunten = new Agenda.GrabCollabAgenda();
            nieuwePunten.Show();
            this.Dispose(false);
        }
    }
}
